package org.leviatan.dataharbour.gui.swingcomponents.views;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.leviatan.dataharbour.core.model.PersistenceResource;
import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlDataSource;

public class DataSourcesPersistenceResourcesPanel extends JPanel {

	private static final long serialVersionUID = -7610974158571014092L;
	
	private JLabel lblTitle;
	
	private JList persResList;
	
	private JScrollPane persResListScrollPane;
	
	private JTree defsTree;
	
	private JToolBar tBar;
	
	private JButton butAdd, butDelete;
	
	
	public DataSourcesPersistenceResourcesPanel() {
		super();
		initialize();
	}
	
	private void initialize() {
		
		lblTitle = new JLabel("Persistence resources in selected datasource");
		persResList = new JList();
		persResListScrollPane = new JScrollPane(persResList);
		tBar = this.initToolBar();
		
		RuntimeContext.persResList = this.persResList;
		//persResList.addListSelectionListener(new DataSourceListListener());
		
		
		///////////////////////////////////////////////////////////////////////
		
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints lblTitleC = new GridBagConstraints();
		GridBagConstraints persResListScrollPaneC = new GridBagConstraints();
		GridBagConstraints tBarC = new GridBagConstraints();
		
		
		tBarC.anchor = GridBagConstraints.NORTHWEST;
		tBarC.fill = GridBagConstraints.BOTH;
		tBarC.gridx = 0;
		tBarC.gridy = 0;
		
		lblTitleC.anchor = GridBagConstraints.NORTHWEST;
		lblTitleC.fill = GridBagConstraints.BOTH;
		lblTitleC.insets = new Insets (10, 5,10,5);
		lblTitleC.gridx = 0;
		lblTitleC.gridy = 1;
		
		persResListScrollPaneC.anchor = GridBagConstraints.NORTHWEST;
		persResListScrollPaneC.insets = new Insets (10, 5, 10 ,5);
		persResListScrollPaneC.fill = GridBagConstraints.BOTH;
		persResListScrollPaneC.gridx = 0;
		persResListScrollPaneC.gridy = 2;
		persResListScrollPaneC.weighty = 1.0;
		persResListScrollPaneC.weightx = 1.0;
		
		/*
		defsTreeC.anchor = GridBagConstraints.NORTHWEST;
		defsTreeC.gridx = 0;
		defsTreeC.gridy = 0;
		*/
		
		this.add(tBar, tBarC);
		this.add(lblTitle, lblTitleC);
		
		//this.add(lblDefsListTitle, lblDefsListTitleC);
		
		this.add(persResListScrollPane, persResListScrollPaneC);
			
	}
	
	private JToolBar initToolBar() {
		
		JToolBar aBar = new JToolBar();
		aBar.setLayout(new GridBagLayout());
		
		GridBagConstraints butCreateTblConstr = new GridBagConstraints();
		butCreateTblConstr.anchor = GridBagConstraints.WEST;
		butCreateTblConstr.insets = new Insets(0, 5, 0, 5);
		butCreateTblConstr.gridx = 0;
		butCreateTblConstr.gridy = 0;
		butCreateTblConstr.fill = GridBagConstraints.BOTH;
	
		GridBagConstraints butDeleteConstr = new GridBagConstraints();
		butDeleteConstr.anchor = GridBagConstraints.WEST;
		butDeleteConstr.insets = new Insets(0, 0, 0, 5);
		butDeleteConstr.gridx = 3;
		butDeleteConstr.gridy = 0;
		butDeleteConstr.weightx = 1.0;
		butDeleteConstr.weighty = 1.0;
		
		GridBagConstraints butCreateRecConstr = new GridBagConstraints();
		butCreateRecConstr.anchor = GridBagConstraints.WEST;
		butCreateRecConstr.insets = new Insets(0, 0, 0, 5);
		butCreateRecConstr.gridx = 1;
		butCreateRecConstr.gridy = 0;
		
		GridBagConstraints butCreateFldConstr = new GridBagConstraints();
		butCreateFldConstr.anchor = GridBagConstraints.WEST;
		butCreateFldConstr.insets = new Insets(0, 0, 0, 5);
		butCreateFldConstr.gridx = 2;
		butCreateFldConstr.gridy = 0;
		
		butAdd = new JButton("Add resource");
		butDelete = new JButton("Remove selected");
		
		butAdd.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				String dsId = getSelectedDsId();
				if (dsId != null && !dsId.equals("")) {
					
					TreeSet<String> persRes = 
						RuntimeContext.dataHarbourService.getDataSourcesPersistenceResources(dsId);
					
					HashMap<String, PersistenceResource> allPersResMap = 
						RuntimeContext.dataHarbourService.getAllPersistenceResources();
					Vector<PersistenceResource> allPersRes = new Vector<PersistenceResource>(allPersResMap.values());
					if (allPersRes == null) allPersRes = new Vector<PersistenceResource>();
					Vector<PersistenceResource> allPersResCloned = 
						(Vector<PersistenceResource>) allPersRes.clone();
					
					
					if (persRes != null && !persRes.isEmpty() ) {
						allPersResCloned.removeAll(persRes);
					}
					
					Object[] possibilities = allPersResCloned.toArray();
					PersistenceResource p = (PersistenceResource)JOptionPane.showInputDialog(
							RuntimeContext.persResList,
					        "Select persistence resource to add to this datasource.",
					        "Select persistence resource",
					        JOptionPane.PLAIN_MESSAGE,
					        new ImageIcon(),
					        possibilities,
					        "");
	
					// String was returned
					if (p != null) {
						RuntimeContext.dataHarbourService.addPersistenceResourceToDataSource(dsId, p.getId());
						updateListData();
					}
	
					// If you're here, the return value was null/empty.
					//setLabel("Come on, finish the sentence!");
					
					RuntimeContext.dataHarbourService.saveDataSource2PersistenceResourceMappings();
				}
			}
		});
		
		butDelete.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				PersistenceResource p = (PersistenceResource) persResList.getSelectedValue();
				//System.out.println("Pers res id: " +  p.getId());
				
				DefaultMutableTreeNode selectedDataSource = (DefaultMutableTreeNode) 
					RuntimeContext.dataSourceTree.getLastSelectedPathComponent();

				if (selectedDataSource == null) return;
			
				TreeNodeMdlDataSource nodeModelDS = (TreeNodeMdlDataSource) 
					selectedDataSource.getUserObject();
				
				RuntimeContext.dataHarbourService.removePersistenceResourceFromDataSource(
						nodeModelDS.getId(), p.getId());
				
				//RuntimeContext.dataHarbourService.saveRegister();
				
				RuntimeContext.dataHarbourService.saveDataSource2PersistenceResourceMappings();
				
				updateListData();
			}
		});
		
		aBar.add(butAdd, butCreateTblConstr);
		aBar.add(butDelete, butDeleteConstr);
		
		aBar.setRollover(true);
		aBar.setFloatable(false);
		
		return aBar;
		
	}
	
	private String getSelectedDsId() {
		DefaultMutableTreeNode selectedDataSource = (DefaultMutableTreeNode) 
			RuntimeContext.dataSourceTree.getLastSelectedPathComponent();

		if (selectedDataSource == null) return null;

		TreeNodeMdlDataSource nodeModelDS = (TreeNodeMdlDataSource) 
			selectedDataSource.getUserObject();
		
		return nodeModelDS.getId();
	}
	
	private void updateListData() {
		if (this.getSelectedDsId() == null) return;
		
		TreeSet<String> persRes = 
			RuntimeContext.dataHarbourService.getDataSourcesPersistenceResources(
					this.getSelectedDsId());
		if (persRes != null)
			RuntimeContext.persResList.setListData(persRes.toArray());
	}

}
